home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / WHO_S.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  8KB  |  286 lines

  1. /*
  2. **  WHO_S.C  [WHOIS Server]
  3. */
  4.  
  5. #include <windows.h>
  6. #include <winsock.h>
  7.  
  8. #include "wil.h"
  9. #include "message.h"
  10. #include "paint.h"
  11. #include "about.h"
  12.  
  13. #ifdef WIN32
  14. #define USE_INS HINSTANCE
  15. #define USE_PTR PSTR
  16. #else
  17. #define USE_INS HANDLE
  18. #define USE_PTR LPSTR
  19. #endif
  20.  
  21. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  22.  
  23. #define FALSE 0
  24. #define TRUE  1
  25.  
  26. #define ONE_SEC  1000
  27. #define TEN_SEC 10000
  28.  
  29. /*  globals */
  30.  
  31. HWND hMainWnd;            /* main window handle */
  32.  
  33. #define LF        10
  34. #define CR        13
  35. #define MAX_BUF  128
  36.  
  37. #define WHOIS_PORT    5001
  38.  
  39. static HMENU hMenu;
  40. static USE_INS hInstance;
  41. static int WinWidth = 8 * NCOLS;
  42. static int WinHeight = 12 * NROWS + 48;
  43. static char Temp[MAX_BUF+8];
  44. static char InBuffer[MAX_BUF+1];
  45. static SOCKET ListenSock = 0;
  46. static SOCKET DataSock = 0;
  47. static LPSTR  HostPtr;
  48. static ULONG  HostAddr = 0;
  49.  
  50. static int AcceptFlag = FALSE;
  51.  
  52. /* miscellaneous functions */
  53.  
  54. static void DisplayError(int Code, LPSTR Msg)
  55. {DisplayString("ERROR: ");
  56.  if(Msg) DisplayString(Msg);
  57.  if(Code)
  58.    {wilErrorText(Code,(LPSTR)Temp,50);
  59.     DisplayLine((LPSTR)Temp);
  60.    }
  61. }
  62.  
  63. /* WinMain */
  64.  
  65. #ifdef WIN32
  66. int WINAPI
  67. #else
  68. int PASCAL
  69. #endif
  70. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  71.         USE_PTR szCmdLine,  int nCmdShow)
  72. {WNDCLASS  wc;
  73.  MSG msg;
  74.  BOOL Result;
  75.  if(!hPrevInstance)
  76.    {/* register main window class */
  77.     wc.style = CS_HREDRAW | CS_VREDRAW;
  78.     wc.lpfnWndProc = MainWndProc;
  79.     wc.cbClsExtra = 0;
  80.     wc.cbWndExtra = 0;
  81.     wc.hInstance = hInst;
  82.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  83.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  84.     wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  85.     wc.lpszMenuName =  "HostMenu";
  86.     wc.lpszClassName = "HostWClass";
  87.     Result = RegisterClass(&wc);
  88.     if(!Result) return FALSE;
  89.    }
  90.  /* create main window */
  91.  hInstance = hInst;
  92.  hMainWnd = CreateWindow(
  93.         "HostWClass",   "WHOIS Server",    WS_OVERLAPPEDWINDOW,
  94.         CW_USEDEFAULT,  CW_USEDEFAULT,
  95.         WinWidth,       WinHeight,
  96.         NULL,           NULL,
  97.         hInstance,      NULL);
  98.  ShowWindow(hMainWnd, nCmdShow);
  99.  UpdateWindow(hMainWnd);
  100.  hMenu = GetMenu(hMainWnd);
  101.  
  102.  /* window control loop */
  103.  
  104.  while(GetMessage(&msg,NULL,0,0))
  105.    {
  106.     TranslateMessage(&msg);
  107.     DispatchMessage(&msg);
  108.    }
  109.  return (msg.wParam);
  110. } /* end WinMain */
  111.  
  112. #ifdef WIN32
  113. LRESULT CALLBACK
  114. #else
  115. long FAR PASCAL
  116. #endif
  117. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  118. {int Code;
  119.  UINT idTimer;
  120.  HDC hDC;
  121.  PAINTSTRUCT ps;
  122. #ifdef WIN32
  123. #else
  124.  static FARPROC lpfnAboutDlgProc;
  125. #endif
  126.  hMainWnd = hWindow;
  127.  switch (iMsg)
  128.     {case WM_CREATE:
  129. #ifdef WIN32
  130. #else
  131.        /* create thunk for Win16 */
  132.        lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  133. #endif
  134.       /* initialize paint module */
  135.       PaintInit();
  136.       /* attach WINSOCK */
  137.       DisplayString("Attaching WINSOCK...");
  138.       Code = wilAttach();
  139.       DisplayLine("OK");
  140.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  141.       else
  142.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  143.          DisplayLine((LPSTR)Temp);
  144.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  145.          DisplayLine((LPSTR)Temp);
  146.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  147.          DisplayLine((LPSTR)Temp);
  148.         }
  149.        /* start "accept" timer */
  150.        idTimer = SetTimer(hMainWnd,1,1000,NULL);
  151.        if(idTimer==0)
  152.          {DisplayLine("ERROR: No timers remaining!");
  153.          }
  154.        break;
  155.  
  156.      case WM_COMMAND:
  157.          switch(wParam)
  158.            {
  159.             case MSG_ABOUT :
  160. #ifdef WIN32
  161.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  162. #else
  163.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  164. #endif
  165.                return 0;
  166.  
  167.             case MSG_EXIT:
  168.               wilRelease();
  169.               DestroyWindow(hMainWnd);
  170.               break;
  171.  
  172.             case MSG_DEBUG:
  173.               Code = wilDebug(0);
  174.               wsprintf((LPSTR)Temp,"Debug(0) returned %d",Code);
  175.               DisplayLine((LPSTR)Temp);
  176.               break;
  177.  
  178.             case MSG_WHOIS:
  179.  
  180.               DisplayLine("Starting WHOIS Server");
  181.               /* get local host domain name */
  182.               HostPtr = wilGetMyHostName();
  183.               if(HostPtr==NULL)
  184.                 {DisplayError(0, "Cannot get host name");
  185.                  break;
  186.                 }
  187.               /* get host IP address */
  188.               HostAddr = wilGetHostAddr(0);
  189.               if(HostAddr==0)
  190.                 {DisplayError(0, "Cannot get IP addess");
  191.                  break;
  192.                 }
  193.               wsprintf((LPSTR)Temp,"HostAddr = %s\n", wilGetHostDotted(0));
  194.               DisplayLine((LPSTR)Temp);
  195.               /* create listener socket */
  196.               ListenSock = wilTcpSocket();
  197.               if((int)ListenSock<=0)
  198.                 {DisplayError((int)Code, "wilListen:");
  199.                  break;
  200.                 }
  201.               /* bind address & port number */
  202.               Code = wilBind(ListenSock, HostAddr, WHOIS_PORT);
  203.               if(Code<=0)
  204.                 {DisplayError((int)Code, "wilBind:");
  205.                  break;
  206.                 }
  207.               /* listen for incoming requests */
  208.               Code = wilListen(ListenSock,1);
  209.               if(Code<=0)
  210.                 {DisplayError(Code, NULL);
  211.                  break;
  212.                 }
  213. #if 1
  214.               wsprintf((LPSTR)Temp,"ListenSock=%d HostAddr=%lx Port=%d",
  215.                                    ListenSock, HostAddr, WHOIS_PORT);
  216.               DisplayLine((LPSTR)Temp);
  217. #endif
  218.               DisplayLine("Listening...");
  219.               AcceptFlag = TRUE;
  220.               break;
  221.            }
  222.          break;
  223.  
  224.      case WM_TIMER:
  225.  
  226.        if(!AcceptFlag) break;
  227.        DisplayChar('?');
  228.        /* any incoming connection ? */
  229.        if(!wilDataIsReady(ListenSock,0)) break;
  230.        DisplayLine("Data incoming...");
  231.        AcceptFlag = FALSE;
  232.        /* accept the connection */
  233.        DataSock = wilAccept(ListenSock,ONE_SEC);
  234.        if((int)DataSock<=0)
  235.          {DisplayError((int)Code, "Accept:");
  236.           break;
  237.          }
  238.        wsprintf((LPSTR)Temp,"DataSock=%d",(int)DataSock);
  239.        DisplayLine((LPSTR)Temp);
  240.        DisplayLine("Connection is accepted");
  241.        /* read socket */
  242.        Code = wilReadString(DataSock,(LPSTR)InBuffer,MAX_BUF);
  243.        wsprintf((LPSTR)Temp,"Whois [%s]", InBuffer);
  244.        DisplayLine((LPSTR) Temp);
  245.        /* formulate & send response */
  246.        if(lstrcmpi(InBuffer,"Mike")==0)
  247.          {wilWriteString(DataSock,"Mike Marshall <mike@marshallsoft.com>\r\n");
  248.           wilCloseSocket(DataSock);
  249.          }
  250.        else if(lstrcmpi(InBuffer,"Pam")==0)
  251.          {wilWriteString(DataSock,"Pam Marshall <pam@marshallsoft.com>\r\n");
  252.           wilCloseSocket(DataSock);
  253.          }
  254.        else if(lstrcmpi(InBuffer,"Lauren")==0)
  255.          {wilWriteString(DataSock,"Lauren Marshall <lauren@marshallsoft.com>\r\n");
  256.           wilCloseSocket(DataSock);
  257.          }
  258.        else
  259.          {wilWriteString(DataSock,"No such user");
  260.           wilCloseSocket(DataSock);
  261.          }
  262.        /* ready for next request */
  263.        AcceptFlag = TRUE;
  264.        break;
  265.  
  266.     case WM_PAINT:
  267.       HideCaret(hMainWnd);
  268.       hDC = BeginPaint(hMainWnd, &ps);
  269.       SetMapMode(hDC,MM_ANISOTROPIC);
  270.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  271.       PaintMain(hDC,&ps);
  272.       EndPaint(hMainWnd,&ps);
  273.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  274.       ShowCaret(hMainWnd);
  275.       break;
  276.  
  277.     case WM_DESTROY:
  278.       PostQuitMessage(0);
  279.       break;
  280.  
  281.     default:
  282.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  283.    }
  284.  return 0;
  285.  
  286. } /* end MainWndProc */